/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.Dataset;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import view.Prediction;


/**
 *
 * @author Fernando
 */
public class PredictionControl {
    
    private double[] temperature;
    private double[] presion;
    private double[] pluviosity;
    private double[] prediction = new double[10];

    public PredictionControl() {
        Init();
    }
    
    public void Init(){
        temperature = new double[view.CentralFrame.Registers.size()];
        presion = new double[view.CentralFrame.Registers.size()];
        pluviosity = new double[view.CentralFrame.Registers.size()];
        for(int i=0; i<view.CentralFrame.Registers.size(); i++){
            temperature[i] = (view.CentralFrame.Registers.get(i).getTemperature());
            presion[i] = (view.CentralFrame.Registers.get(i).getAtmPressure());
            pluviosity[i] = (view.CentralFrame.Registers.get(i).getPluviosity());
        }
    }
    
    public double[] lineal(double[] data){
        double pxy, sx, sy, sx2, sy2;
        double[] res = new double[2];
        pxy=sx=sy=sx2=sy2=0.0;
        for(int i=0; i<data.length; i++){
            sx+=(i+1);
            sy+=data[i];
            sx2+=(i+1)*(i+1);
            sy2+=data[i]*data[i];
            pxy+=(i+1)*data[i];
        }
        res[0]=(data.length*pxy-sx*sy)/(data.length*sx2-sx*sx);
        res[1]=(sy-res[0]*sx)/data.length;
        return res;
    }
    
    public void GraphicTemperature(){
        XYSeries series = new XYSeries("Proyeccion");        
        
        double[] res = lineal(temperature);
        
        for (int i = 0; i < (temperature.length)+10; i++) {
            series.add(i+1,(res[0]*(i+1))+res[1]);       
        }
                
        XYDataset juegoDatos= new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createXYLineChart("","Tiempo","Temperatura",juegoDatos,PlotOrientation.VERTICAL,false, false,true);
        try {
        ChartUtilities.saveChartAsJPEG(new File("Temp.jpg"), chart, 500, 300);
        } catch (IOException e) {
        System.err.println("Error creando grafico.");
        }
       
    }
    
    public void GraphicPressure(){
        XYSeries series = new XYSeries("Proyeccion");        
        
        double[] res = lineal(presion);

        for (int i = 0; i < (presion.length)+10; i++) {
            series.add(i+1,(res[0]*(i+1))+res[1]);       
        }
                
        XYDataset juegoDatos= new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createXYLineChart("","Tiempo","Presion",juegoDatos,PlotOrientation.VERTICAL,false, false,true);
        try {
        ChartUtilities.saveChartAsJPEG(new File("Press.jpg"), chart, 500, 300);
        } catch (IOException e) {
        System.err.println("Error creando grafico.");
        }
        
    }
    
    public void GraphicPluviosity(){
        XYSeries series = new XYSeries("Proyeccion");        
        
        double[] res = lineal(pluviosity);
        
        for (int i = 0; i < (pluviosity.length)+10; i++) {
            series.add(i+1,(res[0]*(i+1))+res[1]);       
        }
                
        XYDataset juegoDatos= new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createXYLineChart("","Tiempo","Pluviosidad",juegoDatos,PlotOrientation.VERTICAL,false, false,true);
        try {
        ChartUtilities.saveChartAsJPEG(new File("Pluv.jpg"), chart, 500, 300);
        } catch (IOException e) {
        System.err.println("Error creando grafico.");
        }
        
        
    }
}